package com.oauth.global.client.ui.birthday;

import java.util.List;
import java.util.Map;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DeckPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Widget;
import com.oauth.components.client.rpc.OAuthAsnyCallBack;
import com.oauth.components.client.ui.HasWidgetInfo;
import com.oauth.components.client.ui.HorizontalFlowPanel;
import com.oauth.global.client.beans.AppInfoBean;
import com.oauth.global.client.beans.BirthdayClientBean;
import com.oauth.global.client.rpc.GlobalRemoteServiceAsync;

public class BirthdayNotification extends Composite {

	private static BirthdayNotificationUiBinder uiBinder = GWT
			.create(BirthdayNotificationUiBinder.class);

	interface BirthdayNotificationUiBinder extends UiBinder<Widget, BirthdayNotification> {
	}

	@UiField
	BirthdayPanel todayPanel;

	@UiField
	BirthdayPanel tomorrowPanel;

	@UiField
	BirthdayPanel monthPanel;

	@UiField
	DeckPanel deckPanel;

	@UiField
	HorizontalFlowPanel loadingPanel;

	private Map<Long, HasWidgetInfo> widgetMap;

	public BirthdayNotification() {
		initWidget(uiBinder.createAndBindUi(this));
		todayPanel.setDayText("Today ");
		tomorrowPanel.setDayText("Tomorrow ");
		monthPanel.setDayText("Next 30 days ");
	}

	/**
	 * @param widgetMap
	 *            the widgetMap to set
	 */
	public void setWidgetMap(Map<Long, HasWidgetInfo> widgetMap) {
		this.widgetMap = widgetMap;
	}

	public void clearAll() {
		todayPanel.clear();
		tomorrowPanel.clear();
		monthPanel.clear();
	}

	public int getTodayCount() {
		return todayPanel.getCount();
	}

	public int getTotalCount() {
		return todayPanel.getCount() + tomorrowPanel.getCount() + monthPanel.getCount();
	}

	public void initBirthdays() {
		clearAll();
		deckPanel.showWidget(0);

		GlobalRemoteServiceAsync.INSTANCE
				.getApplicationImagesForBirthday(new OAuthAsnyCallBack<List<AppInfoBean>>() {

					@Override
					public void onSuccess(List<AppInfoBean> pAppInfoList) {
						deckPanel.showWidget(1);
						if (pAppInfoList == null || pAppInfoList.isEmpty()) {
							deckPanel.showWidget(2);
						} else {
							for (AppInfoBean infoBean : pAppInfoList) {
								Image appImage = widgetMap.get(infoBean.getId()).getIcon();
								BirthdayLoadingPanel appLoading = new BirthdayLoadingPanel(appImage);
								loadingPanel.add(appLoading);
								initBirthday(infoBean.getId(), appImage, appLoading);
							}
						}

					}
				});
	}

	public void initBirthday(final Long appId, final Image appImage,
			final BirthdayLoadingPanel pAppLoading) {
		// deckPanel.showWidget(0);
		GlobalRemoteServiceAsync.INSTANCE.getUpcomingBirthdays(appId,
				new OAuthAsnyCallBack<List<BirthdayClientBean>>() {

					@Override
					public void onSuccess(List<BirthdayClientBean> pbirthdayList) {
						pAppLoading.setVisible(false);
						if (pbirthdayList == null || pbirthdayList.isEmpty()) {
							// memberPanel.add(new
							// HTML("No upcoming birthday's"));
						} else {
							for (BirthdayClientBean birthday : pbirthdayList) {
								birthday.setAppIconUrl(appImage.getUrl());
								if (birthday.isToday()) {
									todayPanel.addMember(birthday);
								} else if (birthday.isTomorrow()) {
									tomorrowPanel.addMember(birthday);
								} else {
									monthPanel.addMember(birthday);
								}
							}
						}
						// deckPanel.showWidget(1);
					}
				});
	}

}
